package com.thinkgem.jeesite.frame.pm.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.persistence.BasePage;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.frame.pm.PmConstants;
import com.thinkgem.jeesite.frame.pm.entity.PmDataDictItem;
import com.thinkgem.jeesite.frame.pm.entity.PmParam;
import com.thinkgem.jeesite.frame.pm.service.PmParamService;
import com.thinkgem.jeesite.frame.pm.utils.PmCacheUtils;
import com.thinkgem.jeesite.frame.pm.utils.PmDataDictUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 参数Controller
 *
 * @author kongxiangsheng
 * @version 2018-09-03
 */
@Controller
@RequestMapping(value = "${adminPath}/pm/pmParam")
public class PmParamController extends BaseController {
    @Autowired
    private PmParamService pmParamService;

    @RequiresPermissions("pm:pmDataTree:view")
    @RequestMapping(value = {""})
    public String index(PmParam pmParam, Model model) {
        return "frame/pm/PmParam/index";
    }

    @RequiresPermissions("pm:pmParam:view")
    @RequestMapping(value = {"list"})
    public String list(PmParam pmParam, HttpServletRequest request, HttpServletResponse response, Model model) {
        BasePage<PmParam> page = new BasePage<PmParam>(request, response);
        pmParamService.page(page, new QueryWrapper<PmParam>(pmParam));

        model.addAttribute("page", page);
        return "frame/pm/PmParam/list";
    }

    @RequiresPermissions("pm:pmParam:view")
    @RequestMapping(value = "form")
    public String form(PmParam pmParam, Model model) {
        if (StringUtils.isNotBlank(pmParam.getId())) {
            pmParam = pmParamService.getById(pmParam.getId());
        }
        model.addAttribute("pmParam", pmParam);
        return "frame/pm/PmParam/form";
    }

    @RequiresPermissions("pm:pmParam:edit")
    @RequestMapping(value = "save")
    public String save(PmParam pmParam, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, pmParam)) {
            return form(pmParam, model);
        }
        pmParamService.saveOrUpdate(pmParam);

        // 清空缓存
        PmCacheUtils.removePmParam(pmParam.getCode());

        addMessage(redirectAttributes, "保存参数成功");
        return "redirect:" + adminPath + "/pm/pmParam/list?repage&classify=" + pmParam.getClassify();
    }

    @RequiresPermissions("pm:pmParam:edit")
    @RequestMapping(value = "delete")
    public String delete(PmParam pmParam, RedirectAttributes redirectAttributes) {
        // 查询参数信息
        pmParam = pmParamService.getById(pmParam.getId());

        // 清空缓存
        PmCacheUtils.removePmParam(pmParam.getCode());

        // 删除数据
        pmParamService.removeById(pmParam.getId());
        addMessage(redirectAttributes, "删除参数成功");
        return "redirect:" + adminPath + "/pm/pmParam/list?repage&classify=" + pmParam.getClassify();
    }

    /**
     * 获取参数类型JSON数据。
     *
     * @param response
     * @return
     */
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeData")
    public List<Map<String, Object>> treeData(HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();

        // 添加父节点
        Map<String, Object> parentMap = Maps.newHashMap();
        parentMap.put("id", "1");
        parentMap.put("pId", "0");
        parentMap.put("pIds", "0,");
        parentMap.put("name", "参数类型");
        parentMap.put("isParent", true);
        mapList.add(parentMap);

        // 添加所有子节点
        List<PmDataDictItem> list = PmDataDictUtils.getDataDictItemList(PmConstants.DICT_PM_PARAM_CLASSIFY);
        for (PmDataDictItem dictItem : list) {
            Map<String, Object> map = Maps.newHashMap();
            map.put("id", dictItem.getItemCode());
            map.put("pId", "1");
            map.put("pIds", "0,1,");
            map.put("name", dictItem.getItemName());
            map.put("isParent", false);
            mapList.add(map);
        }
        return mapList;
    }

}